package com.service.admin;

import com.bean.bo.orders.ChangeStateBO;
import com.bean.bo.orders.ShowAllBO;
import com.bean.vo.Order.*;
import com.dao.admin.GoodsMapper;
import com.dao.admin.OrderMapper;
import com.google.gson.Gson;
import com.utils.GsonUtils;
import com.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author: 晓帆
 * @description:
 * @create: 2021-06-18 21:08
 **/
public class OrderServiceImplement implements OrderService{
    @Override
    public Integer deleteOrder(Integer id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        int deleteState = mapper.deleteOrderById(id);
        sqlSession.close();
        if (deleteState==1){
            return 200;
        }
        return 404;
    }

    @Override
    public OrderVO showAllOrders(ShowAllBO showAllBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        OrderVO orderVO = new OrderVO();
        List<ListObjInOrderVO> listObjInOrderVOS = mapper.selectListObjInOrderVOLimit5(showAllBO);
        orderVO.setOrders(listObjInOrderVOS);
        int total =mapper.selectCountFromOrders(showAllBO);
        sqlSession.close();
        orderVO.setTotal(total);
        return orderVO;
    }

    @Override
    public UpdateOrderVO showUpdateOrder(Integer id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        UpdateOrderVO up = mapper.selectUpdateOrderVOById(id);
        UpdateOrderVO updateOrderVO1 = new UpdateOrderVO(up.getId(),up.getAmount(),up.getNum(),up.getGoodsDetailId(),up.getState(),up.getGoods(),up.getSpec(),up.getStates(),up.getCurState(),up.getCurSpec());

        sqlSession.close();
        return updateOrderVO1;
    }

    @Override
    public int changeState(ChangeStateBO changeStateBO) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        int updateState = mapper.updateStateByChangeStateBO(changeStateBO);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long l = System.currentTimeMillis();
        Date date = new Date(l);
        String time = sf.format(date);
        changeStateBO.setUpdateTime(time);
        int i = mapper.updateTime(changeStateBO);
        sqlSession.close();
        if (updateState==1&&i!=0){
            return 200;
        }
        return 404;
    }
}
